package com.bsb.biz.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.bsb.base.mapper.SysRoleMenuMapper;
import com.bsb.base.model.SysRoleMenu;
import com.bsb.base.model.SysRoleMenuExample;
import com.bsb.base.model.SysRoleMenuExample.Criteria;

@Service
public class SysRoleMenuService{
	
	@Resource
	private SysRoleMenuMapper sysRoleMenuMapper;
	
	public void addSysRoleMenu(SysRoleMenu sysRoleMenu) {
		sysRoleMenuMapper.insertSelective(sysRoleMenu);
	}
	
	public void delSysRoleMenuById(Long id) {
		sysRoleMenuMapper.deleteByPrimaryKey(id);
	}
	
	public void updateSysRoleMenu(SysRoleMenu sysRoleMenu) {
		sysRoleMenuMapper.updateByPrimaryKeySelective(sysRoleMenu);
	}
	
	public void updateSysRoleMenuByExample(SysRoleMenu sysRoleMenu,SysRoleMenuExample sysRoleMenuExample){
	    sysRoleMenuMapper.updateByExampleSelective(sysRoleMenu,sysRoleMenuExample);
	}
	
	public SysRoleMenu getSysRoleMenuById(Long id) {
		return sysRoleMenuMapper.selectByPrimaryKey(id);
	}
	
	public List<SysRoleMenu> getPageSysRoleMenu(SysRoleMenuExample sysRoleMenuExample) {
		return sysRoleMenuMapper.selectPage(sysRoleMenuExample);
	}
	
	public int countSysRoleMenu(SysRoleMenuExample sysRoleMenuExample) {
		return sysRoleMenuMapper.countByExample(sysRoleMenuExample);
	}
	
	public List<SysRoleMenu> selectByExample(SysRoleMenuExample example){
	    return sysRoleMenuMapper.selectByExample(example);
	}
	
	public void insertBatchRoleMenu(Long id, List<String> menulist) {
		for (String menuid : menulist) {
			SysRoleMenu rolemunu = new SysRoleMenu();
			rolemunu.setMenu_id(Long.valueOf(menuid));
			rolemunu.setRole_id(id);
			sysRoleMenuMapper.insertSelective(rolemunu);
		}
	}
	
	public void delByExample(SysRoleMenuExample sysRoleMenuExample){
		sysRoleMenuMapper.deleteByExample(sysRoleMenuExample);
	}
	
	public List<Long> getMenuListByRoleId(Long id){
		List<Long> menuList = new ArrayList<Long>();
		SysRoleMenuExample example = new SysRoleMenuExample();
		Criteria criteria = example.createCriteria();
		criteria.andRole_idEqualTo(id);
		List<SysRoleMenu> list = sysRoleMenuMapper.selectByExample(example);
		for (SysRoleMenu sysRoleMenu : list) {
			Long menu_id = sysRoleMenu.getMenu_id();
			menuList.add(menu_id);
		}
		return menuList;
	}
	
}
